﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// 生产收货信息
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ProductionReceivingInfoController : ControllerBase
    {
        private readonly ILogger<WMSProductionReceivingInfoRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;


        public ProductionReceivingInfoController(ILogger<WMSProductionReceivingInfoRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
            con= _configuration.GetConnectionString("WMSDbConnection");
            //con = _configuration.GetConnectionString("WMSDbConnection_Test");

        }

        /// <summary>
        /// WMS生产收货明细查询
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostWMSProductionReceivingInfo([FromBody] WMSProductionReceivingInfoRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            WMSProductionReceivingInfoRequestDto result = new WMSProductionReceivingInfoRequestDto();
            WMSProductionReceivingInfoResponseDto re = new WMSProductionReceivingInfoResponseDto();
            string warehouse_code = strparm.warehouse_code;
            string material_code = strparm.material_code;
            string source_batch = strparm.source_batch;
            string product_warehousing_no = strparm.product_warehousing_no;
            //框架查询方法
            //var data = _WMSTokenCheckService.GetTokenCheckData(supplier_code);

            try
            {
                strSql = new StringBuilder();
                strSql.Append(@"select pro.product_warehousing_no,pro.warehouse_code,pro.warehouse_name,bar.barcode_no,bar.material_code,bar.material_name,bar.material_amount,bar.material_unit,pro.create_time 
from pro_production_receiving pro inner join barcode_file bar  on pro.product_warehousing_no= bar.source_no where bar.create_time>'2022-08-29'  and bar.ancestors=''");

                if (warehouse_code.Trim() != "")
                {
                    strSql.Append(" and pro.warehouse_code='" + warehouse_code.Trim() + "'");
                }
                if (material_code.Trim() != "")
                {
                    strSql.Append(" and pro.material_code='" + material_code.Trim() + "'");
                }
                if (source_batch.Trim() != "")
                {
                    strSql.Append(" and pro.source_batch like '%" + source_batch.Trim() + "%'");
                }
                if (product_warehousing_no.Trim() != "")
                {
                    strSql.Append(" and pro.product_warehousing_no like '%" + product_warehousing_no.Trim() + "%'");
                }



                DataTable dt_barcodecheck = DB.Query(strSql.ToString(), con);
                if (dt_barcodecheck.Rows.Count<=0)
                {
                    strMsg = "WMS系统不存在《"+ warehouse_code + "》库存信息，请检查清楚再调用接口";
                }
                else
                {
                    re.data = DB.TableToListModel<WMSProductionReceivingInfoResponseData>(dt_barcodecheck);
                }
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

         


            if (strMsg=="")
            {
                re.status = "成功";
                re.code = "1";
                re.message = "";
            }
            else
            {
                re.status = "失败";
                re.code = "0";
                re.message = strMsg;
            }


            return new JsonResult(re);
        }





    }
}
